﻿Option Strict On
Imports Kudos2Mod.Common
Imports Kudos2Mod.CSV
Imports FreeImageAPI

Public Class AssetEditor
    Dim AssetsDataset As DataSet
    Dim vbCommon As New Common
    Private Sub MainMenuButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MainMenuButton.Click
        Me.Close()
    End Sub

    Private Sub AssetEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadListBox(FilesPath & "\simulation\assets.csv", AssetsListBox)
        LoadTriggerComboBox()
        LoadStoreComboBox()
        LoadTextureComboBox()
        LoadSoundComboBox()
    End Sub

    Private Sub LoadTriggerComboBox()
        TriggerComboBox.Items.Add("sim_dog")
        TriggerComboBox.Items.Add("sim_dogfood")
        TriggerComboBox.Items.Add("sim_cat")
        TriggerComboBox.Items.Add("sim_catfood")
        TriggerComboBox.Items.Add("sim_fish")
        TriggerComboBox.Items.Add("sim_fishfood")
        TriggerComboBox.Items.Add("sim_book")
        TriggerComboBox.Items.Add("sim_gift")
    End Sub
    Private Sub LoadStoreComboBox()
        StoreComboBox.Items.Add("pets")
        StoreComboBox.Items.Add("general")
        StoreComboBox.Items.Add("books")
        StoreComboBox.Items.Add("music")
    End Sub
    Private Sub LoadTextureComboBox()
        vbCommon.LoadFileList(FilesPath & "\bitmaps\assets", extensionTypes.DdsFiles, TextureComboBox)
    End Sub
    Private Sub LoadSoundComboBox()
        vbCommon.LoadFileList(FilesPath & "\sounds", extensionTypes.OggFiles, SoundComboBox)
    End Sub

    Private Sub LoadListBox(ByVal filepath As String, ByVal listbox As ListBox)
        AssetsDataset = CSV.GetDataSet(filepath, 21)
        For Each DR As DataRow In AssetsDataset.Tables("data").Rows
            listbox.Items.Add(DR("guiname").ToString)
        Next
        LoadFormFields(0)
    End Sub

    Private Sub AssetsListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AssetsListBox.SelectedIndexChanged
        'Load the appropriate selectedindex to the form
        LoadFormFields(AssetsListBox.SelectedIndex)
    End Sub

    Private Sub LoadFormFields(ByVal i As Integer)
        Dim DR As DataRow
        ClearAllFields()
        DR = AssetsDataset.Tables("data").Rows(i)
        TriggerComboBox.Text = DR("trigger").ToString
        GuiNameTextBox.Text = DR("guiname").ToString
        NameLabel.Text = DR("name").ToString
        StoreComboBox.Text = DR("store").ToString
        DescriptionTextBox.Text = DR("description").ToString
        TextureComboBox.Text = DR("texture").ToString
        ViewTextureButton.PerformClick()
        SoundComboBox.Text = DR("sound").ToString
        ResellPctTextBox.Text = DR("resell percentage").ToString
        CostTextBox.Text = DR("purchasecost").ToString
        DailyCostTextBox.Text = DR("dailycost").ToString
        BoughtScriptTextBox.Text = DR("boughtscript").ToString
        OwnedScriptTextBox.Text = DR("ownedscript").ToString
        UsedScriptTextBox.Text = DR("usedscript").ToString
        SetFlags(DR("flags").ToString)
        If DR("trigger").ToString = "sim_book" Then
            SetBookTraits(DR)
        End If
    End Sub

    Private Sub SetBookTraits(ByVal dr As DataRow)
        BookPagesTextBox.Text = dr(15).ToString
        BookLaunchTaskTextBox.Text = dr(16).ToString
        Dim i As Integer
        For i = 18 To dr.Table.Columns.Count - 1
            If dr(i).ToString <> "" Then
                BookPreReqsTextBox.Text &= """" & dr(i).ToString & ""","
            End If
        Next
    End Sub

    Private Sub ClearAllFields()
        RebuyableFlagCheckBox.Checked = False
        SellableFlagCheckBox.Checked = False
        StealableFlagCheckBox.Checked = False
        MeansOfTransportFlagCheckBox.Checked = False
        ServiceFlagCheckBox.Checked = False
        TriggerComboBox.SelectedIndex = -1
        GuiNameTextBox.Text = ""
        NameLabel.Text = ""
        StoreComboBox.SelectedIndex = -1
        DescriptionTextBox.Text = ""
        TextureComboBox.SelectedIndex = -1
        TexturePictureBox.Image = Nothing
        SoundComboBox.SelectedIndex = -1
        ResellPctTextBox.Text = ""
        CostTextBox.Text = ""
        DailyCostTextBox.Text = ""
        BoughtScriptTextBox.Text = ""
        OwnedScriptTextBox.Text = ""
        UsedScriptTextBox.Text = ""
        BookLaunchTaskTextBox.Text = ""
        BookPagesTextBox.Text = ""
        BookPreReqsTextBox.Text = ""
    End Sub

    Private Sub SetFlags(ByVal strFlags As String)
        Dim aFlags(8) As String
        aFlags = Split(strFlags, ",")
        For Each sFlag As String In aFlags
            Select Case sFlag
                Case "REBUYABLE"
                    RebuyableFlagCheckBox.Checked = True
                Case "SELLABLE"
                    SellableFlagCheckBox.Checked = True
                Case "STEALABLE"
                    StealableFlagCheckBox.Checked = True
                Case "SERVICE"
                    ServiceFlagCheckBox.Checked = True
                Case "MEANSOFTRANSPORT"
                    MeansOfTransportFlagCheckBox.Checked = True
            End Select
        Next
    End Sub

    'trigger
    'name
    'guiname
    'store
    'description
    'texture
    'sound
    'resell percentage
    'purchasecost
    'dailycost
    'boughtscript
    'ownedscript
    'usedscript
    'flags

    Private Sub ViewTextureButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ViewTextureButton.Click
        vbCommon.DrawImageOnPictureBox(FilesPath & "\bitmaps\assets\" & TextureComboBox.Text, FREE_IMAGE_FORMAT.FIF_DDS, TexturePictureBox)
    End Sub


End Class